CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
SET(project_name "basic")
PROJECT(${project_name})
#message(${project_name})
#message(${PROJECT_SOURCE_DIR})
#message(${PROJECT_BINARY_DIR})

# config
OPTION(test "test case" ON)
OPTION(debug "debug" OFF)
OPTION(ccfile "compile_commands.json" ON)
SET(ROOT_SOURCE ${PROJECT_SOURCE_DIR}/src)
SET(ROOT_INCLUDE ${PROJECT_SOURCE_DIR}/include)
SET(ROOT_INSTALL ${PROJECT_SOURCE_DIR}/lib)
SET(ROOT_TEST ${PROJECT_SOURCE_DIR}/test)

if(ccfile)
  SET(CMAKE_EXPORT_COMPILE_COMMANDS ON)
endif()

# flag
SET(CMAKE_C_COMPILER /usr/bin/clang)
SET(CMAKE_CXX_COMPILER /usr/bin/clang++)
if (MSVC)
  MESSAGE("[custom] MSVC")
  SET(ROOT_GTEST_LIB ${PROJECT_SOURCE_DIR}/external/googletest/)
  SET(CMAKE_CXX_FLAGS "/w /Od /LD")
elseif (CMAKE_COMPILER_IS_GNUCXX)
  MESSAGE("[custom] GNU")
  SET(ROOT_GTEST_LIB /opt/googletest)
  SET(CMAKE_CXX_FLAGS "-stdlib=libstdc++ -std=c++11 -Wall -lstdc++ -lpthread")
else ()
  MESSAGE("[custom] unknown compiler !!")
endif ()

if(debug)
  ADD_DEFINITIONS(-DM_GLOBAL_DEBUG)
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -g")
else()
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -s")
endif()
MESSAGE("[custom] final compile command line is: ${CMAKE_CXX_FLAGS}")



# source & headers & install;
INCLUDE_DIRECTORIES(${ROOT_INCLUDE})
INCLUDE_DIRECTORIES(${ROOT_SOURCE}/cute_headers)
list(APPEND source_files ${ROOT_SOURCE}/hs.cpp)
list(APPEND source_files ${ROOT_SOURCE}/log.cpp)
list(APPEND source_files ${ROOT_SOURCE}/memory_pool.cpp)
#list(APPEND source_files ${ROOT_SOURCE}/task_executor_old.cpp)
list(APPEND source_files ${ROOT_SOURCE}/task_executor.cpp)
#list(APPEND source_files ${ROOT_SOURCE}/timer.cpp)
list(APPEND source_files ${ROOT_SOURCE}/url.cpp)
list(APPEND source_files ${ROOT_SOURCE}/utils.cpp)
ADD_LIBRARY(${project_name} SHARED ${source_files})
#SET_TARGET_PROPERTIES(${project_name} PROPERTIES PREFIX "") 
INSTALL(TARGETS ${project_name} DESTINATION ${ROOT_INSTALL})


# test
if (test)
  MESSAGE("[custom] test enabled.")
  SET(test_output_file_name "test_${project_name}")
  INCLUDE_DIRECTORIES(${ROOT_TEST})
  INCLUDE_DIRECTORIES(${ROOT_GTEST_LIB}/include)
if (MSVC)
  LINK_DIRECTORIES(${ROOT_GTEST_LIB}/lib/windows-x64/)
else()
  LINK_DIRECTORIES(${ROOT_GTEST_LIB}/lib)
endif()
  ADD_EXECUTABLE(${test_output_file_name} ${ROOT_TEST}/main.cpp)
  TARGET_LINK_LIBRARIES(${test_output_file_name} ${project_name} gtest)
endif()
